package org.lb.MarketingSystem.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.lb.MarketingSystem.dao.CompanyUserMapperDao;
import org.lb.MarketingSystem.entity.CompanyUser;
import org.lb.MarketingSystem.entity.Position;
import org.lb.MarketingSystem.service.CompanyUserService;
import org.lb.MarketingSystem.service.DepartmentService;
import org.lb.MarketingSystem.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "companyUserServiceImpl")
public class CompanyUserServiceImpl implements CompanyUserService {

	@Autowired
	CompanyUserMapperDao companyUserMapperDao;
	@Autowired
	DepartmentService departmentServiceImpl;
	@Autowired
	PositionService positionServiceImpl;
	
	// 根据账号密码得到员工信息
	public CompanyUser getCompanyUser(String accountnumber, String password) {
		CompanyUser companyUser = new CompanyUser();
		if (companyUserMapperDao.selectCompanyUser(accountnumber, password) != null) {
			companyUser = prefectedCompanyuser(companyUserMapperDao.selectCompanyUser(accountnumber, password));
			return companyUser;
		} else {
			return null;
		}

	}

	// 得到所有员工信息
	public List<CompanyUser> get_allCompanyUser() {
		List<CompanyUser> companyUsers = new ArrayList<CompanyUser>();
		for (CompanyUser companyUser : companyUserMapperDao.select_allCompanyUser()) {
			companyUsers.add(prefectedCompanyuser(companyUser));
		}
		return companyUsers;
	}

	// 添加员工
	public CompanyUser addCompanyUser(CompanyUser companyUser) {
		companyUserMapperDao.insertCompanyUser(companyUser);
		CompanyUser companyUser2 = companyUserMapperDao.selectCompanyUserByAccount(companyUser.getAccountNumber());
		companyUserMapperDao.insertToDepartment(companyUser2.getId(),
				departmentServiceImpl.getdepartmentIdByName(companyUser.getDepartment()));
		companyUserMapperDao.insertToPosition(companyUser2.getId(),
				positionServiceImpl.getPositionIdByPositionName(companyUser.getPosition()));
		CompanyUser companyUser3 = prefectedCompanyuser(companyUser2);
		return companyUser3;
	}

	// 根据姓名得到员工信息
	public List<CompanyUser> getCompanyUserByName(String name) {
		List<CompanyUser> companyUsers = new ArrayList<CompanyUser>();
		if (companyUserMapperDao.selectCompanyUserByName(name).size() > 0) {
			for (CompanyUser companyUser : companyUserMapperDao.selectCompanyUserByName(name)) {
				companyUsers.add(prefectedCompanyuser(companyUser));
			}
			return companyUsers;
		} else {
			return null;
		}
	}

	public List<CompanyUser> getCompanyUserBySalary(double salary1, double salary2) {
		return null;
	}

	// 根据部得到员工信息
	public List<CompanyUser> getCompanyUserByDepartmentName(String departmentName) {
		List<CompanyUser> companyUsers = get_allCompanyUser();
		List<CompanyUser> companyUsers2 = new ArrayList<CompanyUser>();
		for (CompanyUser companyUser : companyUsers) {
			if (companyUser.getDepartment().equals(departmentName)) {
				companyUsers2.add(companyUser);
			}
		}
		return companyUsers2;
	}

	public List<CompanyUser> getCompanyUserByPositionName(String positionName) {
		return null;

	}

	// 根据ID删除员工
	public void deleteCompanyUser(Integer userId) {
		companyUserMapperDao.deleteToDepartment(userId);
		companyUserMapperDao.deleteToPosition(userId);
		companyUserMapperDao.dropCompanyUser(userId);
	}

	// 修改员工
	public void updateCompanyUser(CompanyUser companyUser) {
		companyUserMapperDao.updateCompanyUser(companyUser.getAccountNumber(), companyUser.getPassword(),
				companyUser.getSex(), companyUser.getIdNumber(), companyUser.getAge(), companyUser.getBirthdate(),
				companyUser.getAddress(), companyUser.getPhonenumber(), companyUser.getSalary(), companyUser.getName());
		companyUserMapperDao.deleteToDepartment(companyUser.getId());
		companyUserMapperDao.deleteToPosition(companyUser.getId());
		companyUserMapperDao.insertToDepartment(companyUser.getId(),
				departmentServiceImpl.getdepartmentIdByName(companyUser.getDepartment()));
		companyUserMapperDao.insertToPosition(companyUser.getId(),
				positionServiceImpl.getPositionIdByPositionName(companyUser.getPosition()));
	}

	public CompanyUser getCompanyUserById(Integer userId) {
		return null;
	}

	// 得到员工的部门和职位
	public CompanyUser prefectedCompanyuser(CompanyUser companyUser) {
		Integer departmentId = companyUserMapperDao.selectDepartmentIdByUserId(companyUser.getId());
		companyUser.setDepartment(departmentServiceImpl.getDepartmentNameByDerId(departmentId));
		Integer positionId = companyUserMapperDao.selectPositionIdByUserId(companyUser.getId());
		companyUser.setPosition(positionServiceImpl.getPositionNameByPoId(positionId));
		return companyUser;
	}

	// 根据账号得到员工
	public CompanyUser getCompanyUserByAccount(String accountnumber) {
		CompanyUser companyUser = new CompanyUser();
		companyUser = companyUserMapperDao.selectCompanyUserByAccount(accountnumber);
		if (companyUser != null) {
			companyUser = prefectedCompanyuser(companyUser);
			return companyUser;
		} else {
			return null;
		}
	}

	// 分页得到员工信息
	public List<CompanyUser> getCompanyuserByPage(Integer pagenum, Integer number) {
		List<CompanyUser> companyUsers = new ArrayList<CompanyUser>();
		for (CompanyUser companyUser : companyUserMapperDao.selectCpmpanyuserByPage(pagenum, number)) {
			companyUsers.add(prefectedCompanyuser(companyUser));
		}
		return companyUsers;
	}

	public Integer getCounts() {
		return companyUserMapperDao.selectCounts();
	}

	// 根据总条数和煤业需要条数得到总的页数
	public Integer getpagenumbers(Integer counts, Integer num) {
		int remainder = counts % num;
		int sumcount;
		if (remainder > 0) {
			sumcount = counts / num + 1;
		} else {
			sumcount = counts / num;
		}
		return sumcount;
	}
}
